package com.itheima.manager.StudentController;

import com.itheima.manager.Student.Student;
import com.itheima.manager.StudentService.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();

    public void start() {
        StudentController studentController = new StudentController();
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    studentController.addStudent();
                    break;
                case "2":
                    studentController.deletStudent();
                    break;
                case "3":
                    studentController.updateStudent();
                    break;
                case "4":
                    studentController.findStudent();
                    break;
                case "5":
                    System.out.println("感谢你的使用");
                    break lo;
                default:
                    System.out.println("你的输入有误，请重新输入");
                    break;
            }
        }
    }

    private void updateStudent() {
        Student[] students = studentService.findStudent();
        if (students==null){
            System.out.println("数据不存在，请添加后再查询");
            return;
        }
        System.out.println("请输入你要修改的数据");
        String id = sc.next();
        boolean flag=studentService.isExist(id);
        if (flag){
            System.out.println("请输入新的姓名");
            String name = sc.next();
            System.out.println("请输入新的年龄");
            String age = sc.next();
            System.out.println("请输入新的生日");
            String birthday = sc.next();
            Student stu = new Student();
            stu.setId(id);
            stu.setAge(age);
            stu.setName(name);
            stu.setBirthday(birthday);
            studentService.updateStudent(stu);
            System.out.println("修改成功！");
        }else {
            System.out.println("没有这个数字，请重新输入。");
        }
    }



    private void deletStudent() {
        Student[] students = studentService.findStudent();
        if (students==null){
            System.out.println("数据不存在，请添加后再查询");
            return;
        }
        System.out.println("请输入你要删除的数据");
        String id = sc.next();
        boolean flag=studentService.deletStudent(id);
        if (flag==false){
            System.out.println("没有这个数字，请重新输入。");
        }else {
            System.out.println("删除成功");
        }
    }

    private void findStudent() {
        Student[] students = studentService.findStudent();
        if (students==null){
            System.out.println("数据不存在，请添加后再查询");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student stus = students[i];
            if (stus != null) {
                System.out.println(stus.getId() + "\t" + stus.getName() + "\t" + stus.getAge() + "\t\t" + stus.getBirthday());
            }
        }
    }

    private void addStudent() {
        System.out.println("请输入学号：");
        String id;
         lo:while (true) {
            id = sc.next();
            boolean flag = studentService.Exist(id);
            if (flag) {
                System.out.println("学号重复，请重新输入");
            }else {
                break lo;
            }

            }
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入年龄：");
        String age = sc.next();
        System.out.println("请输入生日：");
        String birthday = sc.next();
        Student stu = new Student(id, name, age, birthday);
        studentService.addStudent(stu);
        System.out.println("添加成功！");
        }
    }

